<template>
    <div>
        <el-card>
            <el-button type="primary" @click="createCode()">生成激活码</el-button>
        </el-card>
        <el-card>
            <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="code" label="激活码" width="380">
          <!-- <template #default="scope">
            {{
              scope.row.category 
            
            }}月
          </template> -->
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="250"
        >
        <template #default="scope">
            {{ formDataMoment(scope.row.createTime) }}
          </template>
      </el-table-column>
        
        <el-table-column prop="outDate" label="过期时间">
          <template #default="scope">
            {{ formDataMoment(scope.row.outDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="useFlag" label="是否使用">
          <template #default="scope">
            {{ scope.row.useFlag ? '已使用' : '未使用' }}
          </template>
        </el-table-column>
        <el-table-column prop="action" label="操作">
          <template #default="scope">
            
            <el-popconfirm
              title="确认删除吗"
              confirm-button-text="确认"
              cancel-button-text="删除"
              @confirm="handleDelete(scope.row._id)"
            >
              <template #reference>
                <el-button size="small" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout="prev, pager, next" :total="total"  @current-change="handleCurrentChange" :page-size="10"/>
        </el-card>
    </div>
</template>
<script setup>
import axios from "axios";
import { ElMessage } from "element-plus";
import {ref,reactive,onMounted,computed} from 'vue'
import moment from 'moment'
const params = ref({
  pageNo:1
})
const total = ref(0)
moment.locale("zh-cn");
const tableData = ref([]);
    const createCode = async () => {
       let res =  await axios.post('/wxapi/activation/adminCrateCode')
       if(res.data.code == 200) {
        await navigator.clipboard.writeText(res.data.data);
        ElMessage.success('激活码获取成功，已自动粘贴')
        getCodeList()
       }else {
        ElMessage.error('获取失败')
       }
       
    }
    onMounted(() => {
      getCodeList()
    })
    const formDataMoment = (date) => {
      return moment(date).format("YYYY-MM-DD HH:mm:ss")
};
const handleCurrentChange = (e) => {
    params.value.pageNo = e
    getCodeList()
();
}
    const getCodeList = async () => {
      let res =  await axios.get('/wxapi/activation/getCodeList',{
        params:params.value
      })
      if(res.data.code == 200) {
        tableData.value = res.data.data.result
        total.value = res.data.data.total
        console.log('tableData',tableData.value)
      }else {
        ElMessage.error('获取失败')
      }
      
    }
   const handleDelete =async (id) => {
    let res =  await axios.post('/wxapi/activation/deleteCode',{
      id:id
    })
    if(res.data.code == 200) {
        ElMessage.success('删除成功')
        getCodeList()
      }else {
        ElMessage.error('删除失败')
      }
   }
</script>
